package com.zs.leymovie.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.zs.leymovie.R;
import com.zs.leymovie.activity.LoginActivity;
import com.zs.leymovie.activity.MovieDetailActivity;
import com.zs.leymovie.activity.SelectActivity;
import com.zs.leymovie.activity.ShowImageActivity;
import com.zs.leymovie.activity.VideoViewActivity;
import com.zs.leymovie.bean.DetailMovieDataEntity;
import com.zs.leymovie.utils.DividerItemDecoration;
import com.zs.leymovie.utils.FastBlur;
import com.zs.leymovie.view.MyListView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/8/19 0019.
 */
public class MoiveDetailAdapter implements View.OnClickListener {

    private Context context;
    private BitmapDisplayConfig config;
    private  Animation animation= null;
    private BitmapUtils bitmapUtils;
    private MovieRecyclerViewAdapter recyclerViewAdapter;
    public ViewHolder vh;
    private DetailMovieDataEntity dataEntity;
    private int totalLines = 0;
    private ArrayList<String> small = new ArrayList<>();
    private ArrayList<String> large = new ArrayList<>();

    public MoiveDetailAdapter(Context context, View view) {
        vh = new ViewHolder(view);
        this.context = context;
        config = new BitmapDisplayConfig();
        config.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        config.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        animation = AnimationUtils.loadAnimation(
                context, R.anim.home_listview_anim);
        config.setAnimation(animation);
        bitmapUtils = new BitmapUtils(context);
    }


    public void getItemView(DetailMovieDataEntity entity) {
        dataEntity = entity;
        //加载影片封面图片,需要这张图片经过高斯处理然后设置为整个影片信息的背景图片
        bitmapUtils.display(vh.movie_img_url, entity.getMovie_img_url(), config,
                new BitmapLoadCallBack() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onLoadCompleted(View view, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        //图片模糊高斯处理
                        vh.movie_img_url.setImageBitmap(bitmap);
                        Drawable drawable = new BitmapDrawable(FastBlur.doBlur(bitmap, 20, false));
                        //Drawable drawable = new BitmapDrawable(FastBlur.blurBitmap(bitmap));
                        vh.layout.setBackground(drawable);
                    }

                    @Override
                    public void onLoadFailed(View view, String s, Drawable drawable) {

                    }
                });
        if (TextUtils.equals(entity.getIs_online(), "1")) {
            if (TextUtils.equals(entity.getMovie_presale(), "0")) {
                vh.ratingBar.setVisibility(View.VISIBLE);
                Double dRating = Double.parseDouble(entity.getMovie_score());
                int iRating = (int) ((dRating / 2.0) * 10);
                float fRating = iRating / 10.0f;
                vh.ratingBar.setRating(fRating);
                vh.score.setText(entity.getMovie_score() + "分");
                vh.alter.setImageResource(R.mipmap.xiangqing_icon_star);
                vh.alter_text.setText("评分");
            } else {
                vh.ratingBar.setVisibility(View.INVISIBLE);
                vh.score.setText(entity.getMovie_want_see_num() + "人想看");
                vh.alter.setImageResource(R.mipmap.xiangqing_icon_alert);
                vh.alter_text.setText("上映提醒");
            }
            vh.price_layout.setVisibility(View.VISIBLE);
        } else {
            vh.ratingBar.setVisibility(View.INVISIBLE);
            vh.score.setText(entity.getMovie_want_see_num() + "人想看");
            vh.price_layout.setVisibility(View.GONE);
        }
        vh.type.setText("类型 ：" + entity.getMovie_type());
        vh.country.setText("地区 ：" + entity.getMovie_country());
        vh.length.setText("片长 ： " + entity.getMovie_length() + "分钟");
        vh.showtime.setText("上映 ：" + entity.getMovie_show_date());
        vh.low_price.setText(entity.getMin_price());
        vh.high_price.setText(entity.getMax_price());
        vh.showNum.setText("有" + entity.getCinema_num() + "家影院上映" + entity.getShow_num() + "场");

        //设置recyclerview
        vh.recyclerview.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL_LIST));
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        vh.recyclerview.setLayoutManager(manager);
        vh.recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerViewAdapter = new MovieRecyclerViewAdapter(context, entity.getPoster_data());
        vh.recyclerview.setAdapter(recyclerViewAdapter);

        vh.img_num.setText(entity.getPoster_data().size() + "");
        vh.director.setText("导演 ：" + entity.getMovie_director());
        vh.cast.setText("主演 ：" + entity.getMovie_cast());
        vh.desc.setText(entity.getMovie_desc());

        for (int i = 0; i < entity.getPoster_data().size(); i++) {
            small.add(entity.getPoster_data().get(i).getSmall_poster_url());
            large.add(entity.getPoster_data().get(i).getOriginal_poster_url());
        }

        vh.paly_btn.setOnClickListener(this);
        vh.like_text.setOnClickListener(this);
        vh.alter.setOnClickListener(this);
        vh.alter_text.setOnClickListener(this);
        vh.choose.setOnClickListener(this);
        vh.say.setOnClickListener(this);
        vh.detail_lv_head_ss.setOnClickListener(this);
        vh.detail_lv_head_like.setOnClickListener(this);
        vh.img_num.setOnClickListener(this);
    }


    public static class ViewHolder {
        @ViewInject(R.id.detail_layout)
        private LinearLayout layout;

        @ViewInject(R.id.movie_img_url)
        private ImageView movie_img_url;

        @ViewInject(R.id.xiangqing_icon_play_btn)
        private ImageView paly_btn;

        @ViewInject(R.id.detail_lv_head_ratingbar)
        private RatingBar ratingBar;

        @ViewInject(R.id.detail_lv_head_score)
        private TextView score;

        @ViewInject(R.id.detail_lv_head_type)
        private TextView type;

        @ViewInject(R.id.detail_lv_head_country)
        private TextView country;

        @ViewInject(R.id.detail_lv_head_length)
        private TextView length;

        @ViewInject(R.id.detail_lv_head_show_date)
        private TextView showtime;

        @ViewInject(R.id.detail_lv_head_like_text)
        private TextView like_text;

        @ViewInject(R.id.detail_lv_head_alter_img)
        private ImageView alter;

        @ViewInject(R.id.detail_lv_head_alter_text)
        private TextView alter_text;

        @ViewInject(R.id.detail_lv_head_low_price)
        private TextView low_price;

        @ViewInject(R.id.detail_lv_head_high_price)
        private TextView high_price;

        @ViewInject(R.id.detail_lv_head_num)
        private TextView showNum;

        @ViewInject(R.id.choose)
        private Button choose;

        @ViewInject(R.id.detail_lv_head_recyclerview)
        private RecyclerView recyclerview;

        @ViewInject(R.id.detail_lv_head_img_num)
        private TextView img_num;

        @ViewInject(R.id.detail_lv_head_director)
        private TextView director;

        @ViewInject(R.id.detail_lv_head_cast)
        private TextView cast;

        @ViewInject(R.id.detail_lv_head_desc)
        private TextView desc;

        @ViewInject(R.id.detail_lv_head_say)
        private TextView say;

        @ViewInject(R.id.price_layout)
        private RelativeLayout price_layout;

        @ViewInject(R.id.movie_detail_listview)
        public MyListView mListView;

        @ViewInject(R.id.detail_lv_head_ss)
        private ImageView detail_lv_head_ss;
        @ViewInject(R.id.detail_lv_head_like)
        private ImageView detail_lv_head_like;

        public ViewHolder(View v) {
            ViewUtils.inject(this, v);
        }

    }

    @OnClick({R.id.xiangqing_icon_play_btn, R.id.detail_lv_head_like_text, R.id.detail_lv_head_alter_img, R.id.detail_lv_head_alter_text, R.id.choose, R.id.detail_lv_head_say, R.id.detail_lv_head_ss,R.id.detail_lv_head_desc})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_lv_head_say:
                Intent intentLogin = new Intent(context, LoginActivity.class);
                context.startActivity(intentLogin);
                break;
            case R.id.xiangqing_icon_play_btn:
                Intent intent = new Intent(context, VideoViewActivity.class);
                intent.putExtra("url", dataEntity.getMovie_trailer_url());
                context.startActivity(intent);
                break;
            case R.id.detail_lv_head_like_text:

                break;
            case R.id.detail_lv_head_alter_img:

                break;
            case R.id.detail_lv_head_alter_text:

                break;
            case R.id.choose:
                Intent intentSelect = new Intent(context, SelectActivity.class);
                intentSelect.putExtra("city_id", ((MovieDetailActivity) context).getCity_id());
                intentSelect.putExtra("movie_id", ((MovieDetailActivity) context).getMovie_id());
                intentSelect.putExtra("movie_name", dataEntity.getMovie_name());
                context.startActivity(intentSelect);
                break;
            case R.id.detail_lv_head_ss:
                getChangeLines();
                clickNum++;
                break;
            case R.id.detail_lv_head_desc:
                getChangeLines();
                clickNum++;
                break;
            case R.id.detail_lv_head_like:
                vh.detail_lv_head_like.setImageResource(R.mipmap.xiangqing_icon_like_p);
                break;
            case R.id.detail_lv_head_img_num:
                Intent intent1 = new Intent(context, ShowImageActivity.class);
                intent1.putStringArrayListExtra("small", small);
                intent1.putExtra("large", large);
                intent1.putExtra("movie_name",dataEntity.getMovie_name());
                context.startActivity(intent1);
                break;
        }
    }

    private int clickNum = 0;

    private void getChangeLines() {

        if (clickNum % 2 == 0) {
            if (totalLines == 0) {
                totalLines = vh.desc.getLineCount();
            }
            vh.desc.setFreezesText(true);
            vh.desc.setLines(totalLines);
            vh.desc.setEllipsize(null);
            vh.detail_lv_head_ss.setImageResource(R.mipmap.xiangqing_icon_gray_arrow_up);
        } else {
            vh.desc.setLines(3);
            vh.desc.setEllipsize(TextUtils.TruncateAt.END);
            vh.detail_lv_head_ss.setImageResource(R.mipmap.xiangqing_icon_gray_arrow_down);
        }
    }
}
